package com.promise.jokerdream.work.signin.after

import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.repository.foldWithApiException
import com.promise.jokerdream.utils.AppContext
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult

// 每日宝箱工作
class DailyChestWork(uin: String): BaseWork(uin) {

    override val name: String
        get() = "每日宝箱"

    override val priority: Int
        get() = 8
    
    private val gameApi = GameApi.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.getInstance(AppContext.get()) }

    override suspend fun executeWork(): WorkResult {
        try {
            // 先查询每日宝箱状态
            val queryResult = gameApi.queryDailyChest()
            return queryResult.foldWithApiException(
                onSuccess = { response ->
                    // 根据查询结果决定是否领取
                    if (response.cuNum >= 24) {
                        val chestResult = gameApi.getDailyChest(0)
                        chestResult.foldWithApiException(
                            onSuccess = { chestResponse ->
                                historyManager.addHistory(name, chestResponse.getDescription())
                            },
                            onFailure = { code, msg ->
                                historyManager.addHistory(name, msg, code)
                            }
                        )
                    }
                    
                    if (response.agNum >= 14) {
                        val chestResult = gameApi.getDailyChest(1)
                        chestResult.foldWithApiException(
                            onSuccess = { chestResponse ->
                                historyManager.addHistory(name, chestResponse.getDescription())
                            },
                            onFailure = { code, msg ->
                                historyManager.addHistory(name, msg, code)
                            }
                        )
                    }
                    
                    if (response.auNum >= 30) {
                        val chestResult = gameApi.getDailyChest(2)
                        chestResult.foldWithApiException(
                            onSuccess = { chestResponse ->
                                historyManager.addHistory(name, chestResponse.getDescription())
                            },
                            onFailure = { code, msg ->
                                historyManager.addHistory(name, msg, code)
                            }
                        )
                    }
                    
                    WorkResult.Success
                },
                onFailure = { code, msg ->
                    historyManager.addHistory(name, msg, code)
                    WorkResult.Failure(Exception(msg), msg)
                }
            )
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            return WorkResult.Failure(e, e.message)
        }
    }
}